<?php
/**
 * 前台会员短信模块
 *
 */
class Sms_mainAction extends Action
{
	/**
	 * 网站统一配置
	 *
	 * @var unknown_type
	 */
	public $Config;
	
	/**
	 * 构造函数
	 *
	 */
	public function _initialize()
	{
		$Config = D('Config');
		$this->Config = $Config->find('config_id=1');
	}
	
	/**
	 * 查询登陆用户的所有短信
	 */
	public function UserIndex()
	{
		EasyACL('Sms_main','UserIndex',$_SESSION);
		//设置查询条件
		import('ORG.Util.HashMap');
		$conditions = new HashMap();
		$conditions->put('euser_id,toeuser_id',array(array('eq',$_SESSION['euser_id']),array('eq',$_SESSION['euser_id']),'OR'));
		//实例化短信
		$SMS = D('Sms_main');
		//统计符合条件的短信数量
		$count = $SMS->count($conditions,'main_id');
		/* 每页显示短信数量 */
		$listRows = 15;
		$fields = 'main_id,euser_id,title,sms_state,created';
		import('ORG.Util.Page');
		$p = new Page($count,$listRows);
		$list = $SMS->xfindall($conditions,$fields,'main_id desc',$p->firstRow .','. $p->listRows);
		$page = $p->show();
		//To templates
		$this->assign('SMS',$list);
		$this->assign('page',$page);
		$this->assign('Config',$this->Config);
		$this->display();
	}
	
	/**
	 * 发送短信
	 */
	public function UserEdit()
	{
		EasyACL('Sms_main','UserEdit',$_SESSION);
		//POST
		if ($this->isPost()) {
			$Input = Input::getInstance();
			$Input->filter('addslashes');
			//查询是否有发送人?
			import('ORG.Util.HashMap');
			$conditions = new HashMap();
			$conditions->put('username',array('EQ',$Input->post('username')));
			
			$Euser = D('Euser');
			$count = $Euser->count($conditions,'euser_id');
			//如果有则继续操作
			if ($count == 1) {
				//说明有这个发送人
				//将发送人信息放入User变量
				$User = $Euser->where($conditions)->field('euser_id,username')->find();
				$SMS = D('Sms_main');
				$SMS->euser_id = $_SESSION['euser_id'];
				$SMS->toeuser_id = $User['euser_id'];
				$SMS->title = $Input->post('title');
				$SMS->content = $Input->post('content');
				$SMS->created = time();
				if ($SMS->add()) {
					return $this->success('发送成功!');
				} else {
					return $this->error('发送失败!');
				}
			} else {
				//如果没有则提示发送失败
				return $this->error('请检查发送人是否填写正确');
			}
		}
		//To templates
		$this->assign('Config',$this->Config);
		$this->display();
	}
	
	/**
	 * 查看某一条短信及回复信息
	 */
	public function UserShow()
	{
		EasyACL('Sms_main','UserShow',$_SESSION);
		if ($this->isPost()) {
			$SMSReply = D('Sms_reply');
			if ($SMSReply->create()) {
				if ($SMSReply->add()) {
					$Input = Input::getInstance();
					$Input->filter('addslashes');
					$Conditions = array();
					$Conditions['main_id'] = $Input->post('main_id');
					$SMS = D('Sms_main');
					$SMS->where($Conditions)->find();
					$SMS->sms_state = 'R';
					$SMS->save();
					return $this->redirect('UserIndex','Sms_main');
				} else {
					return $this->error('尝试回复短信失败!');
				}
			} else {
				return $this->error($SMSReply->getError());
			}
		}
		
		$Input = Input::getInstance();
		$Input->filter('addslashes');
		//获取要查看的短信
		$SMS = D('Sms_main');
		$conditions = array();
		$conditions['main_id'] = $Input->get('main_id');
		$SMSContent = $SMS->where($conditions)->xfind();
		unset($SMS);
		//判断是否需要修改短信状态
		//如果短信状态等于未读(N) || 或者是等于有新回复(R) 就进行修改状态
		if ($SMSContent['sms_state'] == 'N' || $SMSContent['sms_state'] == 'R') {
			$SMS = D('Sms_main');
			$SMS->where($conditions)->find();
			$SMS->sms_state = 'Y';
			$SMS->save();
			unset($SMS);
		}
		
		//To templates
		$this->assign('main_id',$Input->get('main_id'));
		$this->assign('SMS',$SMSContent);
		$this->assign('Config',$this->Config);
		$this->display();
	}
	/**
	 * 删除短信
	 */
	public function UserDel()
	{
		EasyACL('Sms_main','UserDel',$_SESSION);
		$Input = Input::getInstance();
		$Input->filter('addslashes');
		
		$SMS = D('Sms_main');
		if ($SMS->deleteById($Input->get('main_id'))) {
			$this->success('成功删除短信');
		} else {
			$this->error('尝试删除短信失败');
		}
	}
	
	/**
	 * 删除多条
	 */
	public function UserDels()
	{
		EasyACL('Sms_main','UserDel',$_SESSION);
		if ($this->isPost()) {
			$ids = $_POST['main_id'];
			import('ORG.Util.HashMap');
			$SMS = D('Sms_main');
			if ($SMS->deleteByIds($ids,NULL,NULL,TRUE)) {
				return $this->success('成功删除!');
			} else {
				return $this->error('尝试删除失败!');
			}
		}
	}
}
?>








